package com.netCross.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

//import com.bin.david.form.annotation.SmartTable;

import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.style.FontStyle;
import com.bin.david.form.data.table.TableData;
import com.bin.david.form.listener.OnColumnItemClickListener;
import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
import com.netCross.myapplication.bean.TrainMain;


import java.util.ArrayList;
import java.util.List;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;


public class TableActivity extends AppCompatActivity {
    private static final String TAG = "PythonOnAndroid";
    private SmartTable<TrainMain> table;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_table);
        initPython();
          List<TrainMain> list = new ArrayList<>();
          List<PyObject>  jsonList= callPythonCode();

          for (int i=0 ;i<jsonList.size();i++){
              String strTypeJson =  String.valueOf(jsonList.get(i));
                JSONObject jsonObject = JSONUtil.parseObj(strTypeJson);
                list.add(new TrainMain(
                        jsonObject.getStr("demand_order_number"),
                        jsonObject.getStr("waybill_id"),
                        jsonObject.getStr("type_of_transport"),
                        jsonObject.getStr("mode_of_transport"),
                        jsonObject.getStr("dangerous_goods_labe"),
                        jsonObject.getStr("transportation_plan_numbe"),
                        jsonObject.getStr("sending_station_telegram_code"),
                        jsonObject.getStr("sending_station_tmis"),
                        jsonObject.getStr("sending_station_name"), "修改"));
          }

        final Column<String> demand_order_number = new Column<>("需求单号", "demand_order_number");
        final Column<String> waybill_id = new Column<>("运单ID", "waybill_id");
        final Column<String> type_of_transport = new Column<>("运输类型", "type_of_transport");
        final Column<String> mode_of_transport = new Column<>("运输方式", "mode_of_transport");
        final Column<String> dangerous_goods_labe = new Column<>("危险品标志", "dangerous_goods_labe");
        final Column<String> transportation_plan_numbe = new Column<>("运输计划号", "transportation_plan_numbe");
        final Column<String> sending_station_telegram_code = new Column<>("发送站电报码", "sending_station_telegram_code");
        final Column<String> sending_station_tmis = new Column<>("发送站tmis", "sending_station_tmis");
        final Column<String> sending_station_name = new Column<>("发送站名称", "sending_station_name");
        final Column<String> updateing = new Column<>("修改按钮", "updateing");




        TableData<TrainMain> tableData = new TableData("车辆主表", list, updateing, demand_order_number, waybill_id, type_of_transport, mode_of_transport,
                dangerous_goods_labe, transportation_plan_numbe, sending_station_telegram_code, sending_station_tmis, sending_station_name);
        table = findViewById(R.id.table);
        updateing.setFixed(true);
        updateing.setOnColumnItemClickListener(new OnColumnItemClickListener<String>() {
            @Override
            public void onClick(Column<String> column, String s, String s2, int i) {
                goToUpdateActivity(demand_order_number.getDatas().get(i));
                Toast.makeText(TableActivity.this, "点击了" + demand_order_number.getDatas().get(i), Toast.LENGTH_SHORT).show();
            }
        });

        table.setTableData(tableData);
        table.getConfig().setContentStyle(new FontStyle(50, Color.BLACK));


    }

    public void goToUpdateActivity(String vaule) {
        Intent intent = new Intent(this, UpdateActivity.class);
        intent.putExtra("demand_order_number", vaule);
        this.startActivity(intent);
    }
 // 初始化Python环境
    void initPython(){
        if (! Python.isStarted()) {
            Python.start(new AndroidPlatform(this));
        }
    }

       List<PyObject> callPythonCode(){
        Python py = Python.getInstance();
       PyObject obj = py.getModule("wbc").get("relodeTable").call();
       List<PyObject> jsonList = obj.asList();
//           Log.d(TAG,jsonList.toString());

           return jsonList;
//         String strTypeJson =  String.valueOf(jsonList.get(0));
//         JSONObject jsonObject = JSONUtil.parseObj(strTypeJson);
//
//         Log.d(TAG,  jsonObject.getStr("demand_order_number"));



    }




}